package se.slackers.floatingtableheader.client.browser;

import com.google.gwt.user.client.Element;

public class DOMUtil {
	/**
	 * Returns the width of the element with margin and padding subtracted.
	 *
	 * @param element
	 * @return
	 */
	public final native int getInnerWidth(Element element) /*-{
		var style = element.currentStyle
				|| element.ownerDocument.defaultView.getComputedStyle(element,
						null);

		var safe = function(v) {
			return parseFloat(v, 10) || 0;
		};

		var border = safe(style.borderLeftWidth) + safe(style.borderRightWidth);
		var padding = safe(style.paddingLeft) + safe(style.paddingRight);
		return element.offsetWidth - padding - border;
	}-*/;

	/**
	 * Returns the height of the element with margin and padding subtracted.
	 *
	 * @param element
	 * @return
	 */
	public final native int getInnerHeight(Element element) /*-{
		var style = element.currentStyle
				|| element.ownerDocument.defaultView.getComputedStyle(element,
						null);

		var safe = function(v) {
			return parseFloat(v, 10) || 0;
		};

		var border = safe(style.borderLeftHeight)
				+ safe(style.borderRightHeight);
		var padding = safe(style.paddingTop) + safe(style.paddingBottom);
		return element.offsetHeight - padding - border;
	}-*/;
}